package com.shuaigexue.exam.service.impl;





import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuaigexue.exam.entity.Banner;
import com.shuaigexue.exam.mapper.BannerMapper;
import com.shuaigexue.exam.service.BannerService;
import com.shuaigexue.exam.service.FileUploadService;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 轮播图服务实现类
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Resource
    private FileUploadService fileUploadService;
    @Override
    public String uploadBannerImage(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 1. 文件的非空校验
        if(file.isEmpty()){
            throw new RuntimeException("上传文件为空");
        }
        // 2. 文件的类型 image /video
        String contentType = file.getContentType();
        if(ObjectUtils.isEmpty(contentType) || !contentType.startsWith("image")){
            throw new RuntimeException("上传文件类型错误");
        }
        // 3. 调用文件上传业务
        // 4. 回显地址
        return fileUploadService.uploadFile("banners",file);
    }
}